package net.dgg.zz.api.credential.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.api.credential.entity.CertificateProductConfiguration;
import net.dgg.zz.api.credential.mapper.CertificateProductConfigurationMapper;
import net.dgg.zz.common.excption.CommonExcption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class CertificateProductConfigurationService {
    @Autowired
    private CertificateProductConfigurationMapper certificateProductConfigurationMapper;

    /**
     * 查询配置数据
     * @param map
     * @return
     */
    public List<CertificateProductConfiguration> getCertificateMatchList(Map<String,Object> map){
        List<CertificateProductConfiguration> list =  certificateProductConfigurationMapper.selectAll(map);
        return list;
    }

    public Integer count(){
        return certificateProductConfigurationMapper.count();
    }



    /**
     * 删除配置数据
     * @param id
     * @return
     */
    public Integer deleteById(Long id){
        return certificateProductConfigurationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 写入配置设置数据
     * @param params
     * @param userEntity
     * @return
     */
    public Integer insert(Map<String,Object> params, UserEntity userEntity){
        Long keyId = DggKeyWorker.nextId();
        Date now = new Date();
        Long productId = Long.parseLong(params.get("productId").toString());
        CertificateProductConfiguration result = certificateProductConfigurationMapper.selectByProductId(productId);
        if (null!=result){
            throw new CommonExcption("该产品已经加入!");
        }
        CertificateProductConfiguration certificateProductConfiguration = new CertificateProductConfiguration();
        certificateProductConfiguration.setId(keyId);
        certificateProductConfiguration.setPrimaryBusinessName("资质");
        certificateProductConfiguration.setSecondBusinessName((String) params.get("secondBusinessName"));
        certificateProductConfiguration.setSecondBusinessId((String) params.get("secondBusinessId"));
        certificateProductConfiguration.setProductName((String) params.get("productName"));
        certificateProductConfiguration.setProductId(Long.parseLong(params.get("productId").toString()));
        certificateProductConfiguration.setProductNo((String) params.get("productId"));
        certificateProductConfiguration.setLastOperatorName(userEntity.getRealName());
        certificateProductConfiguration.setOperation("0");
        certificateProductConfiguration.setLastOperationTime(now);
        certificateProductConfiguration.setGmtCreatedTime(now);
        certificateProductConfiguration.setGmtModifiedTime(now);
        return certificateProductConfigurationMapper.insert(certificateProductConfiguration);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    public CertificateProductConfiguration getCertificateMatchingById(Long id) {
        return certificateProductConfigurationMapper.getCertificateMatchingById(id);
    }

    /**
     * 修改配置数据
     * @param params
     * @param userEntity
     */
    public void updateCertificateMatching(Map params, UserEntity userEntity) {
        Date now = new Date();
        CertificateProductConfiguration certificateProductConfiguration = new CertificateProductConfiguration();
        certificateProductConfiguration.setId(Long.valueOf(params.get("id").toString()));
        certificateProductConfiguration.setPrimaryBusinessName("资质");
        certificateProductConfiguration.setOperation((params.get("operation")).equals("0")?"1":"0");
        certificateProductConfiguration.setSecondBusinessName((String) params.get("secondBusinessForms"));
        certificateProductConfiguration.setProductName((String) params.get("productName"));
        certificateProductConfiguration.setProductNo((String) params.get("productNo"));
        certificateProductConfiguration.setLastOperatorName(userEntity.getRealName());
        certificateProductConfiguration.setLastOperationTime(now);
        certificateProductConfiguration.setGmtModifiedTime(now);
        certificateProductConfigurationMapper.updateCertificateMatching(certificateProductConfiguration);
    }

    /**
     * 根据产品ID查询配置
     * @return
     */
    public CertificateProductConfiguration selectByProductId(Long productId){
        return certificateProductConfigurationMapper.selectByProductId(productId);
    }

    /**
     * 根据产品ID查询配置
     * @return
     */
    public CertificateProductConfiguration selectQyByProductId(Long productId){
        return certificateProductConfigurationMapper.selectQyByProductId(productId);
    }
}
